import { Context, Service } from 'egg'
import { Repository } from './uow'

/**
 * 数据操作基类，每个继承的service可单独实现特性函数或覆盖基类函数
 */
export class EntityService extends Service {
  repository: Repository
  constructor(ctx: Context, repositoryName: string) {
    super(ctx)
    this.repository = this.app.uow[repositoryName]
    // this.logger.info('EntityService.constructor', repositoryName, super.name)
  }

  async all(options) {
    return this.repository.all(options)
  }

  async count(options) {
    return this.repository.count(options)
  }

  async sum(field, options) {
    return this.repository.sum(field, options)
  }

  async list(options) {
    return this.repository.list(options)
  }

  async findOne(options) {
    return this.repository.findOne(options)
  }

  async find(options) {
    return this.repository.find(options)
  }

  async build(options?) {
    return this.repository.build(options)
  }

  async create(options) {
    return this.repository.create(options)
  }

  async update(options, updates) {
    return this.repository.update(options, updates)
  }

  async del(options) {
    return this.repository.del(options)
  }
}
